package com.qhgrain.api.menurolepermission.provider;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.paas.base.modules.utils.date.DateUtil;
import com.paas.base.modules.utils.lang.StringUtil;
import com.pubframework.common.vo.RestVo;
import com.qhgrain.api.menurolepermission.entity.Menu;
import com.qhgrain.api.menurolepermission.service.IMenuService;
import com.qhgrain.gway.util.UUIDUtil;

@Service
public class MenuServiceProvider implements com.qhgrain.api.menurolepermission.IMenuService {

	@Autowired
	private IMenuService menuServiceImpl;
	
	@Override
	public Response addMenu(String menuName, String menuUrl,
			String parentMenuId, String appId, String status, String orderNum) {
		RestVo vo = new RestVo();
		try {
			
			String	createDate = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
			String menuId = UUIDUtil.getUUID();
			int level = 1;
			String path ="ROOT-"+ menuId;
			if(StringUtil.isNotBlank(parentMenuId)){
				Menu parent = this.menuServiceImpl.find(parentMenuId);
				if(parent != null){
					level = level + Integer.parseInt(parent.getLevelNum());
					path = parent.getPath()+"-"+menuId;
				}
			}
			Menu menu = new Menu(menuId,
					menuName,menuUrl,parentMenuId,status,createDate,level+"",orderNum,appId);
			menu.setPath(path);
			this.menuServiceImpl.insert(menu);
			vo.setSuccess(menuId);
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}

	@Override
	public Response editMenu(String menuId, String menuName, String menuUrl,
			String parentMenuId, String appId, String status, String orderNum) {
		RestVo vo = new RestVo();
		try {
			
			Menu menu = this.menuServiceImpl.find(menuId);
			if(menu == null){
				vo.setFail("menuId对应的菜单不存在!");
			}else{
				menu.setName(menuName);
				menu.setMenuUrl(menuUrl);
				menu.setParentMenuId(parentMenuId);
				menu.setAppId(appId);
				menu.setStatus(status);
				menu.setOrderNum(orderNum);
				this.menuServiceImpl.update(menu);
				vo.setSuccess("");
			}
				
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}

	

	@Override
	public Response delete(String menuIds) {
		RestVo vo = new RestVo();
		try {
			List<String> list = Arrays.asList(menuIds.split(","));
			this.menuServiceImpl.delete(list);
			vo.setSuccess("");
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}

	@Override
	public Response get(String menuId) {
		RestVo vo = new RestVo();
		try {
			Menu m = this.menuServiceImpl.find(menuId);
			if(m == null){
				vo.setFail("根据menuId找不到对应的对象");
			}else{
				vo.setSuccess(m);
			}
			
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}

	

	@Override
	public Response queryChildMenus(String menuId,String userId,String status,String isQueryAll) {
		RestVo vo = new RestVo();
		try {
			List<Menu> list;
			if("true".equals(isQueryAll)){
				list = this.menuServiceImpl.queryAllChildMenu(menuId, userId, status);
			}else{
				list = this.menuServiceImpl.queryNextChildMenu(menuId, userId, status);
			}
			
			vo.setSuccess(list);
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}

	@Override
	public Response queryTopMenus(String appId,String userId,String status) {
		RestVo vo = new RestVo();
		try {
			List<Menu> list = this.menuServiceImpl.queryTopLevelMenu(appId,userId,status);
			vo.setSuccess(list);
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}

	@Override
	public Response updateStatus(String menuIds, String status) {
		RestVo vo = new RestVo();
		try {
			if("0".equals(status) || "1".equals(status)){
				List<String> ids = Arrays.asList(menuIds.split(","));
				this.menuServiceImpl.updateStatus(ids, status);
				vo.setSuccess("");
			}else{
				vo.setFail("status只能是0或1");
			}
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}

	@Override
	public Response queryAll(String appId, String userId, String status) {
		RestVo vo = new RestVo();
		try {
			List<Menu> list = this.menuServiceImpl.queryAll(appId, userId, status);
			vo.setSuccess(list);
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}

	@Override
	public Response queryParentMenus(String menuId) {
		RestVo vo = new RestVo();
		try {
			List<Menu> list = this.menuServiceImpl.queryParentMenus(menuId);
			vo.setSuccess(list);
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}

	

	

}
